package com.tywj.panda.customer.common.tool

import android.app.Activity
import android.content.Intent
import androidx.annotation.StringRes
import com.tywj.lib.core.common.manager.ActivityManager
import com.tywj.lib.core.common.util.AppUtil
import com.tywj.lib.core.common.util.LogUtil
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.core.common.util.ToastUtil.shortToast
import com.tywj.lib.route.RouteUtil
import com.tywj.panda.customer.common.http.PDStringCallback
import com.tywj.panda.customer.common.manager.AuthorityManager
import com.tywj.panda.customer.common.provider.ILaunchService
import org.jetbrains.anko.doAsync

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/16
 *     desc   : 退出登录
 *     version: 1.0
 * </pre>
 */
object LogoutTool {

    val isLogin: Boolean
        get() = AuthorityManager.token.isNotEmpty()

    /**
     * 检查当前是否处于登录状态
     */
    fun checkLoginStatus(activity: Activity, action: () -> Unit) {
        if (isLogin) {
            action.invoke()
        } else {
            RouteUtil.getServiceProvider(ILaunchService::class.java)
                ?.toLoginByPhone()
                ?.navigation(activity)
        }
    }

    fun logout(@StringRes descStringResId: Int) {
        logout(ResourcesUtil.getString(descStringResId))
    }

    fun logout(desc: String? = null, token: String = AuthorityManager.token, isClear: Boolean = true) {
        val activity = ActivityManager.getCurrentActivity()
        if (activity != null) {
            val result = ActivityManager.removeActivity(activity)
            if (!result) {
                // 移除失败，说明当前界面是主界面
                ActivityManager.setMainActivity(null)
            }
        }

        // 在显示登录界面后，在关闭当前界面，防止跳转到登录界面很生硬
        RouteUtil.getServiceProvider(ILaunchService::class.java)
            ?.toLoginByPhone()
            ?.let {
                if (activity == null) {
                    it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                it.navigation(activity ?: AppUtil.context) {
                    activity?.finish()
                }
            }

        if (desc != null) {
            shortToast(desc)
        }

        if (isClear) {
            //清除token
            RouteUtil.getServiceProvider(ILaunchService::class.java)
                ?.logout()?.execute(object : PDStringCallback(true) {
                    override fun success(msg: String) {
                    }

                    override fun error(code: Int, message: String) {
                        super.error(code, message)
                        LogUtil.d("logout fail $message")
                    }
                })
        }

        doAsync {
            // 清空缓存
            CacheTool.localCache.clear()
            AuthorityManager.clear()
            ActivityManager.closeAllActivity()
            AuthorityTool.clear()
            // 清空别名
//            RouteUtil.getServiceProvider(IPushService::class.java)
//                ?.clearAlias()
        }
    }
}